package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.IssueOperationRecord;

/**
 * 问题操作记录Mapper接口
 * 
 * @author system
 * @date 2025-11-21
 */
@Mapper
public interface IssueOperationRecordMapper {
    /**
     * 查询问题操作记录
     * 
     * @param id 问题操作记录主键
     * @return 问题操作记录
     */
    @Select("SELECT r.id, r.issue_id, r.project_id, r.operator_id, r.operator_name, " +
            "r.operation_type, r.operation_content, r.operation_time, r.customer_name, " +
            "r.requirement_name, r.remarks, r.create_by, r.create_time, r.update_by, r.update_time " +
            "FROM t_issue_operation_records r WHERE r.id = #{id}")
    IssueOperationRecord selectIssueOperationRecordById(Long id);

    /**
     * 查询问题操作记录列表
     * 
     * @param issueOperationRecord 问题操作记录
     * @return 问题操作记录集合
     */
    @Select("<script>" +
            "SELECT r.id, r.issue_id, r.project_id, r.operator_id, r.operator_name, " +
            "r.operation_type, r.operation_content, r.operation_time, r.customer_name, " +
            "r.requirement_name, r.remarks, r.create_by, r.create_time, r.update_by, r.update_time " +
            "FROM t_issue_operation_records r WHERE 1=1 " +
            "<if test=\"issueId != null\"> AND r.issue_id = #{issueId} </if>" +
            "<if test=\"projectId != null\"> AND r.project_id = #{projectId} </if>" +
            "<if test=\"operatorId != null\"> AND r.operator_id = #{operatorId} </if>" +
            "<if test=\"operationType != null\"> AND r.operation_type = #{operationType} </if>" +
            "<if test=\"customerName != null and customerName != ''\"> AND r.customer_name LIKE CONCAT('%', #{customerName}, '%') </if>" +
            "<if test=\"requirementName != null and requirementName != ''\"> AND r.requirement_name LIKE CONCAT('%', #{requirementName}, '%') </if>" +
            "ORDER BY r.operation_time DESC" +
            "</script>")
    List<IssueOperationRecord> selectIssueOperationRecordList(IssueOperationRecord issueOperationRecord);

    /**
     * 根据问题ID查询操作记录列表
     * 
     * @param issueId 问题ID
     * @return 操作记录集合
     */
    @Select("SELECT r.id, r.issue_id, r.project_id, r.operator_id, r.operator_name, " +
            "r.operation_type, r.operation_content, r.operation_time, r.customer_name, " +
            "r.requirement_name, r.remarks, r.create_by, r.create_time, r.update_by, r.update_time " +
            "FROM t_issue_operation_records r WHERE r.issue_id = #{issueId} ORDER BY r.operation_time DESC")
    List<IssueOperationRecord> selectRecordsByIssueId(Long issueId);

    /**
     * 根据项目ID查询操作记录列表
     * 
     * @param projectId 项目ID
     * @return 操作记录集合
     */
    @Select("SELECT r.id, r.issue_id, r.project_id, r.operator_id, r.operator_name, " +
            "r.operation_type, r.operation_content, r.operation_time, r.customer_name, " +
            "r.requirement_name, r.remarks, r.create_by, r.create_time, r.update_by, r.update_time " +
            "FROM t_issue_operation_records r WHERE r.project_id = #{projectId} ORDER BY r.operation_time DESC")
    List<IssueOperationRecord> selectRecordsByProjectId(Long projectId);

    /**
     * 新增问题操作记录
     * 
     * @param issueOperationRecord 问题操作记录
     * @return 结果
     */
    @Insert("INSERT INTO t_issue_operation_records " +
            "(issue_id, project_id, operator_id, operator_name, operation_type, operation_content, " +
            "operation_time, customer_name, requirement_name, remarks, create_by, create_time) " +
            "VALUES (#{issueId}, #{projectId}, #{operatorId}, #{operatorName}, #{operationType}, " +
            "#{operationContent}, #{operationTime}, #{customerName}, #{requirementName}, " +
            "#{remarks}, #{createBy}, #{createTime})")
    int insertIssueOperationRecord(IssueOperationRecord issueOperationRecord);

    /**
     * 修改问题操作记录
     * 
     * @param issueOperationRecord 问题操作记录
     * @return 结果
     */
    @Update("UPDATE t_issue_operation_records SET " +
            "issue_id = #{issueId}, project_id = #{projectId}, operator_id = #{operatorId}, " +
            "operator_name = #{operatorName}, operation_type = #{operationType}, " +
            "operation_content = #{operationContent}, operation_time = #{operationTime}, " +
            "customer_name = #{customerName}, requirement_name = #{requirementName}, " +
            "remarks = #{remarks}, update_by = #{updateBy}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateIssueOperationRecord(IssueOperationRecord issueOperationRecord);

    /**
     * 删除问题操作记录
     * 
     * @param id 问题操作记录主键
     * @return 结果
     */
    @Delete("DELETE FROM t_issue_operation_records WHERE id = #{id}")
    int deleteIssueOperationRecordById(Long id);

    /**
     * 批量删除问题操作记录
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Delete("<script>" +
            "DELETE FROM t_issue_operation_records WHERE id IN " +
            "<foreach item='id' collection='array' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteIssueOperationRecordByIds(Long[] ids);
}